// pages/withdrawal/withdrawal.js
import {GetWalletInfoApi,businessWalletInfoApi,GetWithdrawApi } from '../../utils/api.js';
import { GetStorewalletinfoApi,storeWalletWithdrawApi } from '../../utils/newApi.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money:'',
    totalMoney:5000,
    show:false,
    id:'',
    incomeInfo:{},//钱包信息
    bankCard:''
  },
  //获取钱包信息
  GetSendCaptcha(){
    let data={
      storeId:this.data.id
    }
    GetStorewalletinfoApi(data).then(res=>{
      this.setData({
        incomeInfo:res.data,
        bankCard:res.data.accountNum.substr(-4)
      })
    })
  },
  //获取钱包信息
  // GetWalletInfo(){
  //   businessWalletInfoApi({id: this.data.id}).then(res=>{
  //     this.setData({
  //       incomeInfo:res.data,
  //       // bankCard:res.data.bankNumber.substr(-4)
  //     })
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
    })
  },
  jumpPage(e){
    if(e.currentTarget.dataset.url){
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    }else {
      wx.showToast({
        title: '功能暂未开放',
        icon:'none',
        duration:2000
      })
    }
  },
  showPopup(e) {
    if (!this.data.money || this.data.money > (this.data.incomeInfo.balance ) || this.data.money < 0) {
      wx.showToast({
        title: '您输入的提现金额有误',
        icon:"none"
      })
      return
    }

    if (this.data.money < 1) {
      wx.showToast({
        title: '提现金额不能小于1元',
        icon:"none"
      })
      return
    }

    if(this.data.incomeInfo.accountNum){
      this.setData({ show: true });
    }else{
      wx.showToast({
        title: '暂未绑定提现银行卡,无法提现',
        icon:'none',
        duration:2000
      })
    }
  },
  getMoney(e){
    this.setData({
      money : e.detail.value
    })
  },
  onClose() {
    this.setData({ show: false });
  },
  // 全部提现
  allWithdrawal(){
    // console.log(this.data.incomeInfo.balance / 100,"00000")
    this.setData({
      money:this.data.incomeInfo.balance
    })
  },
    // 去提现
   cashOut(e){
      let that = this

      console.log(this.data.money,"",this.data.incomeInfo.balance)
  
      if (!this.data.money || this.data.money > (this.data.incomeInfo.balance ) || this.data.money < 0) {
        wx.showToast({
          title: '您输入的提现金额有误',
          icon:"none"
        })
        return
      }
  
      if (this.data.money < 1) {
        wx.showToast({
          title: '提现金额不能小于1元',
          icon:"none"
        })
        return
      }

      let data={
        amount:Number(this.data.money),
        platform:'store',
        storeId:this.data.id,
      }

      storeWalletWithdrawApi(data).then(res=>{
        if(res.code==0){
          console.log("成功成功成功")
          this.setData({
            show:false
          })
          wx.navigateTo({
            url:'/pages/applyResult/applyResult?type=2'
          });
        }else{
          console.log("失败失败失败")
          
          wx.showToast({
            title: res.message,
            icon:"none"
          })
        }
      })
  
      // wx.navigateTo({
      //   url: e.currentTarget.dataset.url
      // });
  
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.GetSendCaptcha();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})